home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / macresource.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  5.6 KB  |  165 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''macresource - Locate and open the resources needed for a script.'''
  5. from Carbon import Res
  6. import os
  7. import sys
  8. import MacOS
  9. import macostools
  10.  
  11. class ArgumentError(TypeError):
  12.     pass
  13.  
  14.  
  15. class ResourceFileNotFoundError(ImportError):
  16.     pass
  17.  
  18.  
  19. def need(restype, resid, filename = None, modname = None):
  20.     '''Open a resource file, if needed. restype and resid
  21.     are required parameters, and identify the resource for which to test. If it
  22.     is available we are done. If it is not available we look for a file filename
  23.     (default: modname with .rsrc appended) either in the same folder as
  24.     where modname was loaded from, or otherwise across sys.path.
  25.     
  26.     Returns the refno of the resource file opened (or None)'''
  27.     if modname is None and filename is None:
  28.         raise ArgumentError, 'Either filename or modname argument (or both) must be given'
  29.     
  30.     if type(resid) is type(1):
  31.         
  32.         try:
  33.             h = Res.GetResource(restype, resid)
  34.         except Res.Error:
  35.             pass
  36.  
  37.         return None
  38.     else:
  39.         
  40.         try:
  41.             h = Res.GetNamedResource(restype, resid)
  42.         except Res.Error:
  43.             pass
  44.  
  45.         return None
  46.     if not filename:
  47.         if '.' in modname:
  48.             filename = modname.split('.')[-1] + '.rsrc'
  49.         else:
  50.             filename = modname + '.rsrc'
  51.     
  52.     searchdirs = []
  53.     if modname == '__main__':
  54.         searchdirs = [
  55.             os.curdir]
  56.     
  57.     if sys.modules.has_key(modname):
  58.         mod = sys.modules[modname]
  59.         if hasattr(mod, '__file__'):
  60.             searchdirs = [
  61.                 os.path.dirname(mod.__file__)]
  62.         
  63.     
  64.     searchdirs.extend(sys.path)
  65.     for dir in searchdirs:
  66.         pathname = os.path.join(dir, filename)
  67.         if os.path.exists(pathname):
  68.             break
  69.             continue
  70.     else:
  71.         raise ResourceFileNotFoundError, filename
  72.     refno = open_pathname(pathname)
  73.     if type(resid) is type(1):
  74.         h = Res.GetResource(restype, resid)
  75.     else:
  76.         h = Res.GetNamedResource(restype, resid)
  77.     return refno
  78.  
  79.  
  80. def open_pathname(pathname, verbose = 0):
  81.     '''Open a resource file given by pathname, possibly decoding an
  82.     AppleSingle file'''
  83.     
  84.     try:
  85.         refno = Res.FSpOpenResFile(pathname, 1)
  86.     except Res.Error:
  87.         arg = None
  88.         if arg[0] in (-37, -39):
  89.             
  90.             try:
  91.                 refno = Res.FSOpenResourceFile(pathname, u'', 1)
  92.             except Res.Error:
  93.                 arg = None
  94.                 if arg[0] != -199:
  95.                     raise 
  96.                 
  97.             except:
  98.                 arg[0] != -199
  99.  
  100.             return refno
  101.             pathname = _decode(pathname, verbose = verbose)
  102.             refno = Res.FSOpenResourceFile(pathname, u'', 1)
  103.         else:
  104.             raise 
  105.     except:
  106.         arg[0] in (-37, -39)
  107.  
  108.     return refno
  109.  
  110.  
  111. def resource_pathname(pathname, verbose = 0):
  112.     '''Return the pathname for a resource file (either DF or RF based).
  113.     If the pathname given already refers to such a file simply return it,
  114.     otherwise first decode it.'''
  115.     
  116.     try:
  117.         refno = Res.FSpOpenResFile(pathname, 1)
  118.         Res.CloseResFile(refno)
  119.     except Res.Error:
  120.         arg = None
  121.         if arg[0] in (-37, -39):
  122.             
  123.             try:
  124.                 refno = Res.FSOpenResourceFile(pathname, u'', 1)
  125.             except Res.Error:
  126.                 arg = None
  127.                 if arg[0] != -199:
  128.                     raise 
  129.                 
  130.             except:
  131.                 arg[0] != -199
  132.  
  133.             return refno
  134.             pathname = _decode(pathname, verbose = verbose)
  135.         else:
  136.             raise 
  137.     except:
  138.         arg[0] in (-37, -39)
  139.  
  140.     return pathname
  141.  
  142.  
  143. def open_error_resource():
  144.     '''Open the resource file containing the error code to error message
  145.     mapping.'''
  146.     need('Estr', 1, filename = 'errors.rsrc', modname = __name__)
  147.  
  148.  
  149. def _decode(pathname, verbose = 0):
  150.     newpathname = pathname + '.df.rsrc'
  151.     if os.path.exists(newpathname) and os.stat(newpathname).st_mtime >= os.stat(pathname).st_mtime:
  152.         return newpathname
  153.     
  154.     if hasattr(os, 'access') and not os.access(os.path.dirname(pathname), os.W_OK | os.X_OK):
  155.         import tempfile
  156.         (fd, newpathname) = tempfile.mkstemp('.rsrc')
  157.     
  158.     if verbose:
  159.         print 'Decoding', pathname, 'to', newpathname
  160.     
  161.     import applesingle
  162.     applesingle.decode(pathname, newpathname, resonly = 1)
  163.     return newpathname
  164.  
  165.